home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / comp / patchup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-09  |  1.5 KB  |  45 lines

  1. /*
  2.                               P A T C H U P . C
  3. */
  4.  
  5. #include "iccomp.h"
  6.  
  7. void patchup(INT8 *code, unsigned len, unsigned *list, unsigned listlen,
  8.              int pos)
  9. {                                           /* list, listlen: list of */
  10.     register unsigned                       /* offsets to patchup     */
  11.         index,
  12.         beyond_jump;
  13.     char
  14.         *cp,                                /* codepointer */
  15.         jumpsize[2];
  16.  
  17.     if (!listlen)                           /* done if nothing to patchup */
  18.         return;
  19.  
  20.     if (pos)
  21.         pos = len;
  22.                                             /* walk all elements to patchup */
  23.     for (index = 0; index < listlen; index++)
  24.     {
  25.         beyond_jump = list[index];
  26.             /*
  27.                 beyond-jump is the offset immediately beyond the jump to
  28.                 the end of the code. This is the position where the jump
  29.                 starts after having read the jump instruction. Hence, the
  30.                 jumpsize is determined by the distance 'pos - beyond-jump',
  31.                 whereas this jumpsize must be inserted 2 bytes earlier
  32.                 than beyond_jump, as part of the jump instruction.
  33.             */
  34.  
  35.                                             /* determine the size of the jmp */
  36.         *(INT16 *)jumpsize = pos - beyond_jump;
  37.         cp = code + beyond_jump - 2;        /* point to codebytes to patch */
  38.  
  39.         *cp = jumpsize[0];                  /* copy byte 0 */
  40.         *(cp + 1) = jumpsize[1];            /* copy byte 1 */
  41.     }
  42.  
  43.     free(list);
  44. }
  45.